package example.dao;

import example.dao.provider.ShipInfoDynaSqlProvider;
import example.pojo.ShipInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

public interface ShipInfoDao {
    // 分页获取航班信息
    @Results({ @Result(id = true, column = "id", property = "id"), @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"), @Result(column = "intro", property = "intro"),
            @Result(column = "startAddress", property = "startAddress"), @Result(column = "endAddress", property = "endAddress"),
            @Result(column = "endTime", property = "endTime"), @Result(column = "startTime", property = "startTime"),
            @Result(column = "price", property = "price"), @Result(column = "intro", property = "intro"),
            @Result(column = "status", property = "status"),
            @Result(column = "tid", property = "type", one = @One(select = "com.example.dao.TypeDao.selectById", fetchType = FetchType.EAGER)) })
    @SelectProvider(type = ShipInfoDynaSqlProvider.class, method = "selectWithParam")
    List<ShipInfo> selectByPage(Map<String, Object> params);
    // 查询总数
    @SelectProvider(type = ShipInfoDynaSqlProvider.class,method = "count")
    Integer count(Map<String, Object> params);

    // 添加
    @Insert("insert into product_info(code,name,tid,brand,pic,num,price,intro,status) "
            + "values(#{code},#{name},#{type.id},#{brand},#{pic},#{num},#{price},#{intro},#{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(ShipInfo pi);

    // 修改
    @Update("update product_info set code=#{code},name=#{name},tid=#{type.id},"
            + "brand=#{brand},pic=#{pic},num=#{num},price=#{price},intro=#{intro}," + "status=#{status} where id=#{id}")
    void edit(ShipInfo pi);

    // 更新状态
    @Update("update product_info set status=#{flag} where id in (${ids})")
    void updateState(@Param("ids") String ids, @Param("flag") int flag);

    // 获取列表
    @Select("select * from product_info where status=1")
    List<ShipInfo> getOnSaleShip();

    // 根据id获取对象
    @Select("select * from product_info where id=#{id}")
    ShipInfo getShipInfoById(int id);
}
